/*

Copyright 2009 Michel Boto. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#include "../PreOrder.h"
#include <list>
#include <tr1/memory>
#include <iostream>
#include <vector>

struct PNode;
typedef std::tr1::shared_ptr<PNode> PNodePtr;

struct PNode
{
	PNode()
	: m_c(0)
	{}
	
	char m_c;
	std::list< PNodePtr > m_kids;
};

typedef std::list<PNodePtr>::iterator ChildPNodeIterator;

struct PNodeIterator : public Blatt::PreOrder<PNodeIterator,PNodePtr,ChildPNodeIterator>
{
	typedef Blatt::PreOrder<PNodeIterator,PNodePtr,ChildPNodeIterator> TBase;
	
	PNodeIterator()
	: TBase() 
	{}
	
	explicit PNodeIterator( PNodePtr& node )
	: TBase( node ) 
	{}
	
	TChildItHolder GetChildRangeForNode( PNodePtr pNode ) const { return TChildItHolder( pNode->m_kids.begin(), pNode->m_kids.end() ); }	
};

bool nodeIsEven( PNodePtr pNode ) { return pNode && ((pNode->m_c - 0) % 2 == 0); }

int main() {
	
	std::cout << "[PreOrder Example] Using shared pointers with a list iterator to the children." << std::endl;
	
	PNodePtr rootPNode( new PNode );
	{
		rootPNode->m_c = '1';
		{
			PNodePtr bNode( new PNode );
			bNode->m_c = '2';
			{
				PNodePtr aNode( new PNode );
				aNode->m_c = '3';
				bNode->m_kids.push_back( aNode );
			}
			{
				PNodePtr dNode( new PNode );
				dNode->m_c = '4';
				{
					PNodePtr cNode( new PNode );
					cNode->m_c = '5';
					dNode->m_kids.push_back( cNode );
				}
				{
					PNodePtr eNode( new PNode );
					eNode->m_c = '6';
					dNode->m_kids.push_back( eNode );
				}
				bNode->m_kids.push_back( dNode );
			}
			rootPNode->m_kids.push_back( bNode );
		}
		{
			PNodePtr gNode( new PNode );
			gNode->m_c = '7';
			{
				PNodePtr iNode( new PNode );
				iNode->m_c = '8';
				{
					PNodePtr hNode( new PNode );
					hNode->m_c = '9';
					iNode->m_kids.push_back( hNode );
				}
				gNode->m_kids.push_back( iNode );
			}
			rootPNode->m_kids.push_back( gNode );
		}
	}
	
	{
		PNodeIterator itPNode( rootPNode );
		const PNodeIterator itPEnd;
		for (; itPNode != itPEnd; ++itPNode )
		{
			std::cout << (*itPNode)->m_c << " ";
		}
		std::cout << std::endl;
	}
	
	std::cout << "[PreOrder Example] copying all odd nodes from the tree into a vector, then printing them out." << std::endl;
	
	PNodeIterator itPNode( rootPNode );
	const PNodeIterator itPEnd;
	std::vector<PNodePtr> oddNodes;
	std::remove_copy_if(
		itPNode,
		itPEnd,
		std::back_inserter( oddNodes ),
		nodeIsEven
	);
	std::vector<PNodePtr>::const_iterator itOdd( oddNodes.begin() );
	const std::vector<PNodePtr>::const_iterator itOddEnd( oddNodes.end() );
	for (; itOdd != itOddEnd; ++itOdd )
		std::cout << (*itOdd)->m_c << " ";
	std::cout << std::endl;
	
	return 0;
}